const mongoose = require('mongoose');

const PersonnelSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    trim: true
  },
  position: {
    type: String,
    default: ''
  },
  rank: {
    type: String,
    default: ''
  },
  organization: {
    type: String,
    required: true,
    trim: true
  },
  imageUrl: {
    type: String,
    default: ''
  },
  detailUrl: {
    type: String,
    default: ''
  },
  sourceId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Source',
    required: true
  },
  fullDetails: {
    // 存储从详情页采集的所有信息
    type: mongoose.Schema.Types.Mixed,
    default: null
  },
  biography: {
    type: String,
    required: false
  },
  detailPageContent: {
    // 存储详情页的HTML内容
    type: String,
    default: null
  },
  detailPageSnapshot: {
    // 存储详情页的截图（二进制数据）
    type: Buffer,
    default: null
  },
  firstDetected: {
    type: Date,
    default: Date.now
  },
  lastUpdated: {
    type: Date,
    default: Date.now
  },
  status: {
    type: String,
    enum: ['active', 'inactive', 'new', 'changed'],
    default: 'active'
  },
  uniqueIdentifier: {
    // 用于识别同一人员的唯一标识符（通常是详情页URL或其他唯一特征）
    type: String,
    required: true,
    unique: true
  },
  createdAt: {
    type: Date,
    default: Date.now
  }
}, { timestamps: true });

// 索引，用于提高查询性能
PersonnelSchema.index({ name: 1, organization: 1 });
PersonnelSchema.index({ uniqueIdentifier: 1 }, { unique: true });
PersonnelSchema.index({ sourceId: 1 });
PersonnelSchema.index({ status: 1 });
PersonnelSchema.index({ name: 'text' });

module.exports = mongoose.model('Personnel', PersonnelSchema); 